package pap.gui;

import java.util.List;

import pap.BufferManager;

public class GUIManager extends Thread {

	private GUI gui;
	private BufferManager buffer;
	private List<IShape> shapes;
	private boolean suspended;
	private boolean stopped;

	public GUIManager(BufferManager buffer) {
		super("GUIManager");
		suspended = false;
		stopped = false;
		gui = new GUI();
		gui.setVisible(true);
		this.buffer = buffer;
	}

	public void run() {
		while (!stopped) {
			try {
				synchronized (this) {
					if (suspended)
						wait();
				}
				drawStep();
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Retrieves the next step to draw from the buffer manager and sends it to
	 * the GUI
	 * 
	 * @throws InterruptedException
	 */
	public void drawStep() throws InterruptedException {
		if (!stopped) {
			shapes = buffer.getNextList();

			gui.drawShape(shapes);
		}
	}

	public void stopGUI() {
		stopped = true;
	}

	public void resumeGUI() {
		suspended = false;
		synchronized (this) {
			notify();
		}
	}

	public void suspendGUI() {
		suspended = true;
	}
}
